

#include "stdint.h"
#include "string.h"
#include "ringbuff.h"
#include "ultra_KS104.h"
#include "uart_comm_ex_api.h"

const uint8_t KS104_measure_cmd[KS104_ALL_NUM][3] = 
{
	{ID_KS104_X0, 0x02, 0x0F},
	{ID_KS104_X1, 0x02, 0x0F},
	{ID_KS104_X2, 0x02, 0x0F},
	{ID_KS104_X3, 0x02, 0x0F},
	{ID_KS104_X4, 0x02, 0x0F},
	{ID_KS104_X5, 0x02, 0x0F},
	{ID_KS104_X6, 0x02, 0x0F},
	{ID_KS104_X7, 0x02, 0x0F},
	{ID_KS104_X8, 0x02, 0x0F},
	{ID_KS104_X9, 0x02, 0x0F}
};



uint8_t  KS104_measure_start = 0;
uint16_t KS104_start_cnt = 0;

STR_KS104_HDL KS104Hdl[KS104_ALL_NUM];

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void KS104_data_analyse(STR_KS104_RAW *rawData)
{
	for(uint8_t i=0; i<KS104_ALL_NUM; i++)
	{
		if(rawData->ID == KS104_measure_cmd[i][0])
		{
			KS104Hdl[i].ID = rawData->ID;
			KS104Hdl[i].NUM = i;
			KS104Hdl[i].distance_mm = ((rawData->dataH << 8) + rawData->dataL) * 10 / 58;
			KS104Hdl[i].comm_cnt = 0;
			KS104Hdl[i].status = FSM_KS104_NORMAL;
			
			upload_ultrasound_data(KS104Hdl[i].NUM, KS104Hdl[i].distance_mm);
			
			break;
		}
	}
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void KS104_measure_send(STR_RINGBUF *bufhdl)
{
	static uint8_t send_cnt = 0;
	
	if(KS104_measure_start == 1)
	{
		ringBufWrite(bufhdl, (char*) &KS104_measure_cmd[send_cnt][0], 3);
		
		send_cnt++;
		send_cnt %= KS104_ALL_NUM;
	}
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void KS104_start_ctrl(uint8_t power_status, uint16_t run_time)
{
	if(KS104_measure_start == 0)
	{
		if(power_status == 1)
		{
			KS104_start_cnt += run_time;
			if(KS104_start_cnt > KS104_START_DELAY)
			{
				KS104_measure_start = 1;
			}
		}
		else
		{
			KS104_start_cnt = 0;
		}		
	}
	else
	{
		if(power_status != 1)
		{
			KS104_measure_start = 0;
			KS104_start_cnt = 0;
		}
	}	
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void KS104_comm_minitor(void)
{
	for(uint8_t i=0; i<KS104_USE_NUM; i++)
	{
		KS104Hdl[i].comm_cnt++;
		
		if(KS104Hdl[i].comm_cnt > KS104_COMM_OVERTIME)
		{
			KS104Hdl[i].NUM = i;
			KS104Hdl[i].distance_mm = 0xFFFF;
			KS104Hdl[i].status = FSM_KS104_FAULT;
			
			upload_ultrasound_data(KS104Hdl[i].NUM, KS104Hdl[i].distance_mm);
			
			KS104Hdl[i].comm_cnt = 0;
		}
	}
}

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void KS104_measure_rece(STR_RINGBUF *bufhdl)
{
	char data_temp[128] = {0};
	
	if(KS104_measure_start == 1)
	{
		STR_KS104_RAW KS104_raw;
		int16_t data_len = 0;
		
		KS104_comm_minitor();
		
		data_len = ringBufRead(bufhdl, data_temp, bufhdl->databufLen);
		
		for(int16_t i=0; i<data_len; i++)
		{
			
			if((data_len - i) < 4)
			{
				break;
			}
			
			else if((i + 3) >= data_len)
			{
				break;
			}
			
			KS104_raw.ID = data_temp[i];
			KS104_raw.dataH = data_temp[i+1];
			KS104_raw.dataL = data_temp[i+2];
			KS104_raw.checksum = KS104_raw.ID + KS104_raw.dataH + KS104_raw.dataL;
			
			if(KS104_raw.checksum == data_temp[i+3])
			{
				KS104_data_analyse(&KS104_raw);
			}
		}
	}
	else
	{
		ringBufRead(bufhdl, data_temp, bufhdl->databufLen);
	}
}


